<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <todo-header @add-Task="addTask" :list="list"></todo-header>
    <todo-main :list="showArr" @dele-List="deleList"></todo-main>
    <todo-footer
      :list="list"
      :listStatus="listStatus"
      @changeStatue="changeStatue"
      @del-Compulted="delCompulted"
    ></todo-footer>
  </section>
</template>

<script>
import TodoHeader from './header'
import TodoMain from './main'
import TodoFooter from './foot'

import '../styles/base.css'
import '../styles/index.css'
export default {
  name: 'MyIndex',
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem('list')) || [],
      listStatus: 'all' // 全部(all)  未完成(compulted)  已完成(incompulted)
    }
  },
  watch: {
    list: {
      deep: true,
      handler(newValue) {
        // console.log(newValue);
        localStorage.setItem('list', JSON.stringify(newValue))
      }
    }
  },
  methods: {
    // 添加功能
    addTask(name) {
      // console.log(name);
      this.list.push({
        id: +new Date(),
        name,
        isDone: false
      })
    },
    // 删除功能
    deleList(id) {
      // console.log(id);
      this.list = this.list.filter(item => item.id !== id)
    },
    // 切换状态
    changeStatue(listStatus) {
      this.listStatus = listStatus
    },
    delCompulted() {
      this.list = this.list.filter(item => !item.isDone)
    }
  },
  computed: {
    showArr() {
      if (this.listStatus === 'compulted') {
        return this.list.filter(item => item.isDone !== false)
      } else if (this.listStatus === 'incompulted') {
        return this.list.filter(item => item.isDone !== true)
      } else return this.list
    }
  }
}
</script>
